414A - Mashmokh and Numbers - CodeForces Solution


constructive algorithms number theory *1500

Please click on ads to support us..

Python Code:

import math

items = input().split(" ")
n = int(items[0])
k = int(items[1])    

cnt = n // 2
if n == 1 or k < cnt:
        if n == 1 and k == 0:
        print(1)
    else:
        print(-1)
elif k == cnt:
        ans = [i + 1 for i in range(n)]
    print(*ans)
else:
    left = k - (cnt - 1)
            
    
    ans = [left, 2 * left]
    i = 1
    while len(ans) < n:
        if i != ans[0] and i != ans[1] and math.gcd(i, ans[-1]) == 1:
            ans.append(i)
        i += 1
    print(*ans)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n,k;
    cin>>n>>k;
    if(n == 1 && k == 0)
    {
        cout<<1;
    }
    if(k<n/2 || (n == 1 && k>0))
    {
        cout<<-1<<endl;
    }
    else if(k == n/2)
    {
        for(int i = 1;i<=n;i++)
        {
            cout<<i<<" ";
        }
    }
    else
    {
        long long int x = n/2;
        long long int a = k-x+1;
        long long int b = 2*(k-x+1);
        cout<<a<<" "<<b<<" ";
        n-=2;
        while(n>0)
        {
            b++;
            cout<<b<<" ";
            n--;
        }
    }
}


Comments

Submit
0 Comments
More Questions

721B - Passwords
1263D - Secret Passwords
1371B - Magical Calendar
1726E - Almost Perfect
1360C - Similar Pairs
900A - Find Extra One
1093D - Beautiful Graph
748A - Santa Claus and a Place in a Class
1511B - GCD Length
676B - Pyramid of Glasses
597A - Divisibility
1632A - ABC
1619D - New Year's Problem
242B - Big Segment
938A - Word Correction
159C - String Manipulation 10
258A - Little Elephant and Bits
1536C - Diluc and Kaeya
1428C - ABBB
1557A - Ezzat and Two Subsequences
255A - Greg's Workout
1059A - Cashier
1389C - Good String
1561A - Simply Strange Sort
1337B - Kana and Dragon Quest game
137C - History
1443C - The Delivery Dilemma
6C - Alice Bob and Chocolate
1077C - Good Array
285B - Find Marble